package multimedia;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.RemoteException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Displayable;

import midlet.VadeMecumMIDlet;
import ui.CreateNoteForm;
import ui.RecordAudioForm;
import ui.VadeMecumImageViewer;
import ui.VadeMecumVideoViewer;
import util.Base64Util;
import webservice.VadeMecumMultimediaServicePortType_Stub;

public class SendToServiceWithContextThread implements Runnable {

	private VadeMecumMIDlet midlet;
	
	private byte[] fileArray;
	
	private Alert alert;
	
	private Displayable previous;

	private String fileExtension;
	
	private long fileId;
	
	public SendToServiceWithContextThread(VadeMecumMIDlet midlet, VadeMecumImageViewer previous, byte[] imageArray, long fileId) {
		this.midlet = midlet;
		this.fileArray = imageArray;
		this.previous = previous;
		this.fileExtension = ".jpg";
		this.fileId = fileId;
		alert = new Alert("", "Picture sent to server with context!", null, AlertType.INFO);
		alert.setTimeout(1500);
	}
	
	public SendToServiceWithContextThread(VadeMecumMIDlet midlet, VadeMecumVideoViewer previous, byte[] videoArray, long fileId) {
		this.midlet = midlet;
		this.fileArray = videoArray;
		this.previous = previous;
		this.fileExtension = ".mpg";
		this.fileId = fileId;
		alert = new Alert("", "Video sent to server with context!", null, AlertType.INFO);
		alert.setTimeout(1500);
	}

	public SendToServiceWithContextThread(VadeMecumMIDlet midlet, RecordAudioForm previous, byte[] audioArray, long fileId) {
		this.midlet = midlet;
		this.fileArray = audioArray;
		this.previous = previous;
		this.fileId = fileId;
		this.fileExtension = ".wav";
		alert = new Alert("", "Audio saved!", null, AlertType.INFO);
		alert.setTimeout(1500);
	}

	public SendToServiceWithContextThread(VadeMecumMIDlet midlet, CreateNoteForm previous, byte[] noteBytes, long fileId) {
		this.fileArray = noteBytes;
		this.previous = previous;
		this.fileId = fileId;
		this.fileExtension = ".txt";
		alert = new Alert("", "Note saved!", null, AlertType.INFO);
		alert.setTimeout(1500);
	}

	public void run() {
		System.out.println("vai mandar com contexto");
		VadeMecumMultimediaServicePortType_Stub stub = new VadeMecumMultimediaServicePortType_Stub();
		try {
			String pictureEncoded = Base64Util.base64Encode(fileArray);
						
			InputStream is = midlet.getClass().getResourceAsStream("/context.xml");
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			int ch;
			while ((ch = is.read()) != -1) {
				bos.write(ch);
			}
			String contextEncoded = Base64Util.base64Encode(bos.toByteArray());
			
			stub.saveWithContext("vm_picture" + fileId + fileExtension, pictureEncoded, contextEncoded);
//			previous.repaint();
			midlet.display.setCurrent(alert, previous);
		} catch (RemoteException re) {
			if ("SAXParseException in response from server".equals(re.getMessage().trim())){
				midlet.display.setCurrent(alert, previous);
			} else {				
				midlet.error(re);
			}
		} catch (IOException ioe) {
			midlet.error(ioe);
		}	
	}

}
